#include <bits/stdc++.h>

using namespace std;
const int maxn=100000,len=7;
int primes_num[maxn],cnt2=0;
bitset<maxn> is_prime;
void eulurSeive(int n)
{
    is_prime.set();
    is_prime[0]=is_prime[1]=0;
    for (int i=2;i<=n ;++i )
    {
        if (is_prime[i])
        {
            primes_num[++cnt2]=i;
        }
        for (int j=1;j<=cnt2&&i*primes_num[j]<=n ;++j )
        {
            is_prime[i*primes_num[j]]=0;
            if (i%primes_num[j]==0)
            {
                break;
            }
        }
    }
}
int str2int(char str[],int radix)
{
    int ans=0,len=strlen(str);
    for (int i=0;i<len ;++i )
    {
        ans=ans*radix+str[i]-'0';
    }
    return ans;
}
int main_1015()
{
    eulurSeive(maxn);
    vector<string> vs;
    char N_str[len]={0},N_rev[len]={0};
    int D;
    while (scanf("%s",N_str)&&N_str[0]!='-')
    {
        scanf("%d",&D);
        strcpy(N_rev,N_str);
        reverse(N_str,N_str+strlen(N_str));
        if (is_prime[str2int(N_str,D)]&&is_prime[str2int(N_rev,D)])
        {
            vs.push_back("Yes");
        }
        else
        {
            vs.push_back("No");
        }
    }
    for(auto i:vs)
    {
        printf("%s\n",i.c_str());
    }
    return 0;
}
